package org.easy.swagger;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author: wxl
 * @CreateTime: 2023/1/17 14:34
 */
@Configuration
@Profile({"dev","test"})
@EnableConfigurationProperties(SwaggerProperties.class)
public class EasySwaggerAutoConfiguration {

    @Resource
    private SwaggerProperties properties;

    @Bean
    public Docket createApi () {
        return new Docket(DocumentationType.OAS_30)
                .enable(properties.isEnable())
                .apiInfo(createApiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo createApiInfo () {
        System.out.println(properties);
        Contact contact = new Contact(properties.getName(), properties.getUrl(), properties.getEmail());
        return new ApiInfoBuilder()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .contact(contact)
                .version(properties.getVersion())
                .build();
    }



}
